###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Email.pm.pl -> Email module                                    #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::Email;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Logs.pm.pl";
require "Mail.pm.pl";

use strict;

require "ControlPanel/Output/Email.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EMAIL");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($Resources, $ResourcesIndex) = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => {
      MAILING_LIST  => ["1"]
    },
    MATCH   => "ANY",
    SORT    => "TITLE",
    BY      => "A-Z"
  );
  
  my %Contacts;
  foreach my $resource (@{ $Resources }) {
    push(@{ $Contacts{$resource->{'CONTACT_EMAIL'}} }, $resource);
  }
  
  my %INPUT;
  
  $INPUT{'CONTACTS'} = \%Contacts;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::Email->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EMAIL");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my @Fields = (
    { name => "SUBJECT"      , required => 1              },
    { name => "MESSAGE"      , required => 1              },
    { name => "HTML"         , required => 0              }
  );

  my (%RECORD, %INPUT, @Error);

  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  my @Emails = $SL::CGI->param('FORM_EMAILS');
  if (scalar(@Emails) < 1) {
    push(@Error, "MISSING-EMAILS");
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  my $Skin = Skin::CP::Email->new();
  my $Mail = Mail->new();

  &Standard::PrintHTMLHeader();  

  unless ($SL::QUERY{'do'}) {
    print $Skin->do();
    return 1;
  }

  my $Log = Logs->new(PRINT => 1);

  my $Exit = eval {
    sub {
      $Log->Footer();
      exit;
    }
  };

  $Log->Header();

  $Log->Line("", "uplevel");

  $Log->Line("Beginning email process.\n\n", "element");
  
  $Log->Line("Getting resources... ", "element");
  my ($Resources, $ResourcesIndex) = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => {
      CONTACT_EMAIL => \@Emails
    },
    MATCH   => "ANY",
    SORT    => "TITLE",
    BY      => "A-Z"
  );

  my %Contacts;
  foreach my $resource (@{ $Resources }) {
    push(@{ $Contacts{$resource->{'CONTACT_EMAIL'}} }, $resource);
  }
  $Log->Line("Done.", "success");
  
  $Log->Line("Sending emails...", "element");
  $Log->Line("", "uplevel");

  foreach my $key (keys %Contacts) {
    $Log->Line("Sending email to \"$key\"... ", "element");
    my %Message = $Skin->email(input => { RECORD => \%RECORD, RESOURCES => $Contacts{$key} });
    $Mail->Send(%Message) || ($Log->Line("Error: $Mail->{'ERROR'}", "error") and &$Exit);
    $Log->Line("Done.", "success");
  }
  
  $Log->Line("", "downlevel");

  $Log->Line("\n\n", "");
  $Log->Line("All done!", "element");
  $Log->Line("", "downlevel");

  &$Exit;
}

1;